<?php
/**
*
* @author Poppertom69 (Tom Martin) tom.martin60@btinternet.com
* @package Post Bot
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('UMIL_AUTO', true);
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

//Just incase they run the install before the edits you never know!
if (!class_exists('pb'))
{
	include($phpbb_root_path . 'includes/functions_post_bot.' . $phpEx);
}

$user->session_begin();
$auth->acl($user->data);
$user->setup();

if (!file_exists($phpbb_root_path . 'umil/umil_auto.' . $phpEx))
{
	trigger_error('Please download the latest UMIL (Unified MOD Install Library) from: <a href="http://www.phpbb.com/mods/umil/">phpBB.com/mods/umil</a>', E_USER_ERROR);
}

// The name of the mod to be displayed during installation.
$mod_name = 'POST_BOT';

/*
* The name of the config variable which will hold the currently installed version
* You do not need to set this yourself, UMIL will handle setting and updating the version itself.
*/
$version_config_name = 'post_bot_version';

$lang_array = array(
	'mods/info_acp_post_bot',
	'mods/post_bot',
);

$language_file = $lang_array;

/*
* Options to display to the user (this is purely optional, if you do not need the options you do not have to set up this variable at all)
* Uses the acp_board style of outputting information, with some extras (such as the 'default' and 'select_user' options)
*/
$options = array();

$all_available_extensions = pb::available_extensions();
foreach ($all_available_extensions as $value)
{	
	$extension 	= strtoupper($value[0]);
	$default 	= (pb::check_extension_status($value[0])) ? true : false;
	$options[$value[0]]	= array('lang' => $extension, 'type' => 'radio:yes_no', 'default' => $default, 'explain' => true);
}

$versions = array(
	'0.1.0' => array(
		'table_add' => array(
			array('phpbb_post_bot', array(
					'COLUMNS'		=> array(
						'post_bot_config'	=> array('VCHAR', ''),
						'post_bot_value'	=> array('MTEXT', ''),
					),
					'PRIMARY_KEY'	=> 'post_bot_config',
					'KEYS'			=> array(),
				),
			),
		),
		
		'config_add' => array(
			array('post_bot_installed', 1),
		),
	),
	
	'0.2.0'	=> array(
		'custom'	=> 'install_extensions',
	),
	
	'0.3.0' => array(
		// Alright, now lets add some modules to the ACP
		'module_add' => array(
			// First, lets add a new category
			array('acp', 'ACP_CAT_DOT_MODS', 'ACP_POST_BOT'),

			// Now we will add the settings and features modes.
			array('acp', 'ACP_POST_BOT', array(
					'module_basename'		=> 'post_bot',
					'modes'					=> array('settings', 'welcome', 'birthday', 'auto_reply'),
				),
			),
		),
	),
	
	'0.3.1'	=> array(
		//No data updates just to codes
	),

	'0.4.0'	=> array(
		'config_add' => array(
			array('post_bot_auto_lc', time(), 1),
		),
		
		'custom'	=> 'rephrase_auto_reply',
	),
	
	'0.5.0'	=> array(
		'custom'	=> 'install_extensions',
		
		// Alright, now lets add some modules to the ACP
		'module_add' => array(
			// Now we will add the settings and features modes.
			array('acp', 'ACP_POST_BOT', array(
					'module_basename'		=> 'post_bot',
					'modes'					=> array('anon'),
				),
			),
		),
	),
	
	'1.0.0'	=> array(
		//No data updates just to codes
	),
	
	'1.0.1'	=> array(
		//No data updates just to codes
	),
	
	'1.0.2'	=> array(
		//No data updates just to codes
	),
);

// Include the UMIL Auto file and everything else will be handled automatically.
include($phpbb_root_path . 'umil/umil_auto.' . $phpEx);

function install_extensions($action, $version)
{
	global $umil;
	
	$pb_available_extensions = pb::available_extensions();
	foreach ($pb_available_extensions as $value)
	{	
		$extension_value = request_var($value[0], 1);	
		if ($action == 'install')
		{
			if (!$umil->config_exists($value[0]))
			{
				$umil->config_add($value[0], $extension_value);
			}
			if ($extension_value == 1)
			{
				pb::install_post_bot_extension($value[0]);
			}
		}
		else if ($action == 'update' && !pb::check_extension_status($value[0]))
		{
			$umil->config_update($value[0], $extension_value);
			if ($extension_value == 1)
			{
				pb::install_post_bot_extension($value[0]);
			}
		}
		else if ($action == 'uninstall')
		{
			if (!$umil->config_exists($value[0]))
			{
				$umil->config_remove($value[0]);
			}
		}		
	}		
}

function rephrase_auto_reply($action, $version)
{
	global $post_bot;
	
	if ($action == 'update' && isset($post_bot['auto_reply_forums']))
	{
		//get data from message
		$message = $post_bot['auto_reply_message_2'];
		$details = 'auto_reply_message_' . $post_bot['auto_reply_forums'];
		
		//delete the old message
		pb::delete_value('auto_reply_message_2');
	
		//set the message to new format
		pb::set_post_bot($details, $message);
		
		//set auto reply forums to new format
		$forums = $post_bot['auto_reply_forums'] . ', ';
		pb::set_post_bot('auto_reply_forums', $forums);	

		//add new values
		pb::set_post_bot('auto_reply_time_forums', '');	
		pb::set_post_bot('auto_reply_checks', '1');
		pb::set_post_bot('auto_reply_after', '1');
	}
}

?>